﻿using System;
using System.Data;
using System.Collections.Generic;
using PPWMS.DAL;
using PPWMS.Model;
using PPWMS.Log;
namespace PPWMS.BLL
{
    /// <summary>
    /// OPLog
    /// </summary>
    public partial class OPLog
    {
        private readonly PPWMS.DAL.OPLog dal = new PPWMS.DAL.OPLog();
        private PPWMS.Log.Logmanagement log = new Logmanagement();
        public OPLog()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string Barcode)
        {
            try
            {
                return dal.Exists(Barcode);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return false;
            }

        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(PPWMS.Model.OPLogModel model)
        {
            try
            {
                return dal.Add(model);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PPWMS.Model.OPLogModel model)
        {
            try
            {
                return dal.Update(model);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Barcode)
        {
            try
            {
                return dal.Delete(Barcode);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Barcodelist)
        {
            try
            {
                return dal.DeleteList(Barcodelist);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PPWMS.Model.OPLogModel GetModel(string Barcode)
        {
            try
            {
                return dal.GetModel(Barcode);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public PPWMS.Model.OPLogModel GetModelByCache(string Barcode)
        {

            string CacheKey = "OPLogModel-" + Barcode;
            object objModel = PPWMS.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(Barcode);
                    if (objModel != null)
                    {
                        int ModelCache = PPWMS.Common.ConfigHelper.GetConfigInt("ModelCache");
                        PPWMS.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch (Exception ex)
                {
                    log.WriteLog(ex.Message.ToString());
                    return null;
                }
            }
            return (PPWMS.Model.OPLogModel)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            try
            {
                return dal.GetList(strWhere);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(OPLogModel model)
        {
            try
            {
                return dal.GetList(model);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return null;
            }
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            try
            {
                return dal.GetList(Top, strWhere, filedOrder);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<PPWMS.Model.OPLogModel> GetModelList(string strWhere)
        {
            try
            {
                DataSet ds = dal.GetList(strWhere);
                return DataTableToList(ds.Tables[0]);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<PPWMS.Model.OPLogModel> DataTableToList(DataTable dt)
        {
            List<PPWMS.Model.OPLogModel> modelList = new List<PPWMS.Model.OPLogModel>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                PPWMS.Model.OPLogModel model;
                try
                {
                    for (int n = 0; n < rowsCount; n++)
                    {
                        model = dal.DataRowToModel(dt.Rows[n]);
                        if (model != null)
                        {
                            modelList.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.WriteLog(ex.Message.ToString());
                    return null;
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            try
            {
                return GetList("");
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return null;
            }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            try
            {
                return dal.GetRecordCount(strWhere);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return -1;
            }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            try
            {
                return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return null;
            }
        }
        #endregion  BasicMethod

    }
}

